package com.wmc.controller;

import com.wmc.pojo.Clazz;
import com.wmc.pojo.ClazzQueryParam;
import com.wmc.pojo.PageResult;
import com.wmc.pojo.Result;
import com.wmc.service.ClazzService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/clazzs")
@RestController
public class ClazzController {
    @Autowired
    private ClazzService clazzService;


    @GetMapping
    public Result page(ClazzQueryParam clazzQueryParam){
        log.info("分页查询");
        PageResult<Clazz> pageResult = clazzService.page(clazzQueryParam);
        return Result.success(pageResult);
    }
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        log.info("删除班级");
        clazzService.deleteById(id);
        return Result.success();
    }

    @PostMapping
    public Result save(@RequestBody Clazz clazz){
        log.info("保存班级");
        clazzService.save(clazz);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        log.info("查询班级");
        Clazz clazz = clazzService.getById(id);
        return Result.success(clazz);
    }

    @PutMapping
    public Result update(@RequestBody Clazz clazz){
        log.info("更新班级");
        //在页面原型中，要求如果该班级下关联的有学生，是不允许删除的，并提示错误信息："对不起, 该班级下有学生, 不能直接删除"

        clazzService.update(clazz);
        return Result.success();
    }

    @GetMapping("/list")
    public Result list(){
        log.info("查询班级列表");
        List<Clazz> list = clazzService.list();
        return Result.success(list);

    }

}
